﻿Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client

Namespace Widgets

    Delegate Sub OnWorkItems_ServerCreatedDelegate(ByVal Server As System.Windows.Forms.ToolStripMenuItem)

    Public Class WorkItems(Of TView As {IWorkItems, Control})
        Inherits WidgetBase(Of TView)

        Private BuildThread As New System.Threading.Thread(AddressOf StartBuild)

        Public Event Started()
        Public Event SelectionChanged(ByVal WorkItem As WorkItem)
        'Public Event ServerBuilt(ByVal Server As ToolStripMenuItem)
        Public Event Finished()

        Private ReadOnly Property Servers() As ArrayList
            Get
                Dim objServers As New ArrayList
                Dim serverNames As String() = RegisteredServers.GetServerNames
                objServers.AddRange(serverNames)
                Return objServers
            End Get
        End Property

        Public Sub New(ByVal View As TView)
            MyBase.New(View)
        End Sub

#Region " Raise Events "

        Private Sub RaiseStartedEvent()
            StartedEvent.DynamicInvoke(Nothing)
        End Sub

        Private Sub RaiseFinishedEvent()
            FinishedEvent.DynamicInvoke(Nothing)
        End Sub

        Private Sub RaiseSelectionChangedEvent(ByVal WorkItem As WorkItem)
            SelectionChangedEvent.DynamicInvoke(WorkItem)
        End Sub

        'Private Sub RaiseServerBuiltEvent(ByVal Server As ToolStripMenuItem)
        '    ServerBuiltEvent.DynamicInvoke(Server)
        'End Sub


#End Region

       

        Public Sub Start()
            If Not BuildThread.IsAlive Then
                BuildThread.Start()
            End If
        End Sub

        Private Sub ServerCreated(ByVal Server As System.Windows.Forms.ToolStripMenuItem)
            If View.InvokeRequired Then
                View.Invoke(New OnWorkItems_ServerCreatedDelegate(AddressOf ServerCreated), Server)
            Else
                View.OnWorkItems_ServerCreated(Server)
            End If
        End Sub

        Private Sub WorkItem_Click(ByVal source As Object, ByVal e As EventArgs)
            Dim tsmiWorkItem As ToolStripMenuItem = source
            Dim WI As WorkItem = tsmiWorkItem.Tag
            RaiseSelectionChangedEvent(WI)
        End Sub

#Region " Menu build"

        Private Sub StartBuild()
            RaiseStartedEvent()
            For Each Server As String In Servers
                Dim TFS As TeamFoundationServer = TeamFoundationServerFactory.GetServer(Server)
                Dim tsmiServer As New ToolStripMenuItem
                tsmiServer.Text = TFS.Name
                tsmiServer.Tag = TFS
                ThreadedBuildProjectList(tsmiServer, TFS)
                'RaiseServerBuiltEvent(tsmiServer)
                ServerCreated(tsmiServer)
                Threading.Thread.Sleep(1000)
            Next
            RaiseFinishedEvent()
        End Sub

        Private Sub ThreadedBuildProjectList(ByVal tsmi As ToolStripMenuItem, ByVal Server As TeamFoundationServer)
            Try
                Dim WIT As WorkItemStore = DirectCast(Server.GetService(GetType(WorkItemStore)), WorkItemStore)
                For Each Project As Project In WIT.Projects
                    Dim tsmiProject As New ToolStripMenuItem
                    tsmiProject.Text = Project.Name
                    tsmiProject.Tag = Project
                    ThreadedBuildQueryList(tsmiProject, Project)
                    tsmi.DropDownItems.Add(tsmiProject)
                Next
            Catch ex As Exception
                ThreadedBuildErrorList(tsmi, ex)
            End Try
        End Sub

        Private Sub ThreadedBuildQueryList(ByVal tsmi As ToolStripMenuItem, ByVal Project As Project)
            Try
                ' Then Load Sub Queries
                '---------------------------
                For Each query As StoredQuery In Project.StoredQueries
                    Dim tsmiQuery As New ToolStripMenuItem
                    tsmiQuery.Text = query.Name
                    tsmiQuery.Tag = query
                    tsmiQuery.ToolTipText = query.Description
                    ThreadedBuildWorkItemList(tsmiQuery, query)
                    tsmi.DropDownItems.Add(tsmiQuery)
                    '---------------------------
                Next
                '---------------------------
            Catch ex As Exception
                ThreadedBuildErrorList(tsmi, ex)
            End Try
        End Sub

        Private Sub ThreadedBuildWorkItemList(ByVal tsmi As ToolStripMenuItem, ByVal query As StoredQuery)
            Try
                ' And load Work Items
                '---------------------------
                Dim context As New Hashtable
                context.Add("project", query.Project.Name)
                Dim WIT As WorkItemStore = DirectCast(query.Project.Store.TeamFoundationServer.GetService(GetType(WorkItemStore)), WorkItemStore)
                For Each workitem As WorkItem In WIT.Query(query.QueryText, context)
                    Dim tsmiWorkItem As New ToolStripMenuItem
                    tsmiWorkItem.Text = workitem.Title
                    tsmiWorkItem.Tag = workitem
                    tsmiWorkItem.ToolTipText = workitem.Description
                    AddHandler tsmiWorkItem.Click, AddressOf WorkItem_Click
                    tsmi.DropDownItems.Add(tsmiWorkItem)
                Next
            Catch ex As Exception
                ThreadedBuildErrorList(tsmi, ex)
            End Try
        End Sub

        Private Sub ThreadedBuildErrorList(ByVal tsmi As ToolStripMenuItem, ByVal ex As Exception)
            Dim tsmiError As New ToolStripMenuItem
            tsmiError.Text = ex.GetType.ToString
            tsmiError.ToolTipText = ex.Message
            If Not ex.InnerException Is Nothing Then
                ThreadedBuildErrorList(tsmiError, ex.InnerException)
            End If
            tsmi.DropDownItems.Add(tsmiError)
        End Sub


#End Region

    End Class

End Namespace